import axios from './axios'
const BMap = window.BMap

// 获取本地的城市信息
const getLocalCity = () => {
    return window.localStorage.getItem("currentCity")
}
// 存放本地城市信息
const setLocalCity = city => {
    window.localStorage.setItem('currentCity', JSON.stringify(city))
}


// 获取当前定位城市
const getCurrentCity = () => {
    return new Promise((resolve, reject) => {
        const localCity = getLocalCity()
        if (localCity) {
            resolve(JSON.parse(localCity))
            return
        }
        var myCity = new BMap.LocalCity();
        console.log(myCity);
        myCity.get(async ({ name }) => {
            console.log({ name });
            // 根据城市名称,获取城市对象的信息
            const res = await axios.get(`/area/info?name=${name}`)
            console.log(res.data.body, "res--------");
            setLocalCity(res.data.body)
            resolve(res.data.body)
        })
    })
}
export { getCurrentCity, setLocalCity, getLocalCity }